package br.com.fiap.trabalho.test;

import java.util.List;
import java.util.Scanner;

import javax.persistence.Persistence;

import br.com.fiap.trabalho.dao.AbstractDAOFactory;
import br.com.fiap.trabalho.dao.JPADAOFactory;
import br.com.fiap.trabalho.dao.MovieDAO;
import br.com.fiap.trabalho.entity.Actor;
import br.com.fiap.trabalho.entity.Category;
import br.com.fiap.trabalho.entity.Movie;
import br.com.fiap.trabalho.entity.Studio;
import br.com.fiap.trabalho.util.JPAAuxiliarDAO;
import br.com.fiap.trabalho.util.TestMovieUtil;

public class MovieTestJPA {
	
	static int numeroDeTeste;
	static JPAAuxiliarDAO JPAUtil =  new JPAAuxiliarDAO();
	
	private static MovieDAO dao;

	public static void main(String[] args) {
		Scanner s = new Scanner(System.in);
	    System.out.println("Digite a quantidade de registros para testar: ");
	    numeroDeTeste = s.nextInt();
	    s.close();
		
		AbstractDAOFactory jpaDAOFactory = new JPADAOFactory();
		dao = jpaDAOFactory.createMovieDao();
		
		Persistence.createEntityManagerFactory("persistenceUnit"); 
		TestMovieUtil.generateBase(numeroDeTeste);
		testInsert(numeroDeTeste);
		testQueries(numeroDeTeste, EnumSelect.SELECT_BY_TITLE);
		testQueries(numeroDeTeste, EnumSelect.SELECT_BY_YEAR);
		testQueries(numeroDeTeste, EnumSelect.SELECT_BY_CATEGORY_NAME);
		testQueries(numeroDeTeste, EnumSelect.SELECT_BY_ACTOR_NAME);
		testQueries(numeroDeTeste, EnumSelect.SELECT_BY_STUDIO_NAME);
		testDelete(numeroDeTeste);
	}
	
	private static void testInsert(int numberOperations){
		
		Movie movie = new Movie();
		TestMovieUtil.fillMovie(movie);
		
		List<Category> cat = movie.getCategory();
		List<Actor> act = movie.getActor();
		Studio std = movie.getStudio();
		
		long startTime = System.currentTimeMillis();
		
		for(int i = 1; i <= numberOperations; i++){
			 movie = new Movie();
			 movie.setTitle("Movie "+i);
			 movie.setActor(act);
			 movie.setCategory(cat);
			 movie.setStudio(std);
			 dao.createMovie(movie);
		}
		
		long spentTime = System.currentTimeMillis() - startTime; 
		System.out.println("Tempo gasto: " + spentTime);
		
	}
	
	private static void testDelete(int numberOperations){
		
		Movie movie;
				
		long startTime = System.currentTimeMillis();
		int amountOfMovie = JPAUtil.selecionaMaxMovie();
		for(int i = amountOfMovie - numberOperations; i <= amountOfMovie; i++){
			 movie = new Movie();
			 movie.setId(i);
			 dao.deleteMovie(movie);
		}
		
		long spentTime = System.currentTimeMillis() - startTime; 
		System.out.println("Tempo gasto: " + spentTime);
		
	}
	
	private static void testQueries(int numberOperations, EnumSelect eSelect){
		
		long startTime = System.currentTimeMillis();
		
		for(int i = 1; i <= numberOperations; i++){
			switch (eSelect) {
			case SELECT_BY_TITLE:
				dao.selectMovieByTitle("Movie1");
				break;
			case SELECT_BY_YEAR:
				dao.selectMovieByYear(2000);
				break;
			case SELECT_BY_STUDIO_NAME:
				dao.selectMovieByStudioName("Studaio");
				break;
			case SELECT_BY_CATEGORY_NAME:
				dao.selectMovieByCategoryName("Category");
				break;
			case SELECT_BY_ACTOR_NAME:
				dao.selectMoviesByActorName("Acator");
				break;
			default:
				break;
			}
		}
		
		long spentTime = System.currentTimeMillis() - startTime; 
		System.out.println("Tempo gasto: " + spentTime);
	}

}
